<?php

declare (strict_types=1);
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */

namespace App\Dao;

use App\Model\User;
use App\Model\UserCash;

/**
 * 用户提现 Dao
 *
 * @author 张昊(2396675557@qq.com)
 * @package App\Dao
 */
class UserCashDao extends Dao
{

    /**
     * 提现状态-提现中
     *
     * @var int
     */
    const CASH_IN = 1;

    /**
     * 提现状态-已到账
     *
     * @var int
     */
    const CASH_COMPLETE = 2;

    /**
     * 提现状态-失败
     *
     * @var int
     */
    const CASH_ERROR = 3;

    /**
     * Get model.
     *
     * @return string
     */
    public function model(): string
    {
        return UserCash::class;
    }

    /**
     * get
     *
     * @param array $map 查询参数
     * @return mixed
     */
    public function get(array $map = [])
    {
        return UserCash::query()
            ->where(function($query) use ($map) {
                return queryWhere($query, $map);
            })
            ->where('user_id', uid())
            ->select('amount','status','created_at','id')
            ->orderByDesc('created_at')
            ->paginate(10);
    }

    /**
     * create
     *
     * @param array $data
     * @return mixed
     */
    public function create(array $data)
    {
        return UserCash::query()->create($data);
    }

    /**
     * delete
     *
     * @param int $cash_id
     * @return int
     */
    public function delete(int $cash_id)
    {
        return UserCash::query()->where('id', $cash_id)->delete();
    }

    /**
     * 获取商家提现记录
     *
     * @param array $map 查询参数
     * @return mixed
     */
    public function getCashInfoByTime( $start_time ,$end_time)
    {
        return UserCash::query()
//            ->where(function($query) use ($map) {
//                return queryWhere($query, $map);
//            })
            ->where('user_id', uid())
            ->where('start_time', '>',$start_time)
            ->where('start_time', '<',$end_time)
            ->select('id','user_id','amount','bank_name','bank_account','start_time','cash_status','status','money')
            ->orderByDesc('created_at')
            ->paginate(10);
    }

    /**
     * 通过ID获取用户提现记录
     *
     * @param int $id ID
     * @param bool $abnormal 是否抛异常
     * @return mixed
     */
    public function first(int $id, bool $abnormal = true)
    {
        $result = UserCash::query()->where('id', $id)->first();

        if (!$result && $abnormal === true) {
            $this->error('WITHDRAWAL.NOT_EXIST');
        }

        return $result;
    }
}